package com.xr.sunnyday.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.xr.sunnyday.dao.MemberDao;
import com.xr.sunnyday.dao.OrderDao;
import com.xr.sunnyday.service.ReportService;
import com.xr.sunnyday.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = ReportService.class)
public class ReportServiceImpl implements ReportService {

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    @Override
    public Map<String, Object> getBusinessReport() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //reportDate 当前时间
        String today = sdf.format(new Date());
        //周日
        String sunnyday = sdf.format(DateUtils.getSundayOfThisWeek());
        //周一
        String monday = sdf.format(DateUtils.getThisWeekMonday());
        //1号
        String firstDayOfThisMonth = sdf.format(DateUtils.getFirstDay4ThisMonth());
        //这个月最后一天
        String lastDayOfThisMonth = sdf.format(DateUtils.getLastDayOfThisMonth());
        // =================== 会员统计 ======================
        //todayNewMember 今日新增会员数
        Integer todayNewMember = memberDao.findMemberCountByDate(today);
        //totalMember 总会员数
        Integer totalMember = memberDao.findMemberTotalCount();
        //thisWeekNewMember 本周新增会员数
        Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(monday);
        //thisMonthNewMember 本月新增会员数
        Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(firstDayOfThisMonth);

        // ===============  预约统计 ===================
        //todayOrderNumber 今日会员数
        Integer todayOrderNumber = orderDao.findOrderCountByDate(today);
        //todayVisitsNumber 今日到诊数
        Integer todayVisitsNumber = orderDao.findVisitsCountByDate(today);
        //thisWeekOrderNumber 本周预约数
        Integer thisWeekOrderNumber = orderDao.findOrderCountBetweenDate(monday,sunnyday);
        //thisWeekVisitsNumber 本周到诊数
        Integer thisWeekVisitsNumber = orderDao.findVisitsCountAfterDate(monday);
        //thisMonthOrderNumber 本月预约数
        Integer thisMonthOrderNumber = orderDao.findOrderCountBetweenDate(firstDayOfThisMonth, lastDayOfThisMonth);
        //thisMonthVisitsNumber 本月到诊数
        Integer thisMonthVisitsNumber = orderDao.findVisitsCountAfterDate(firstDayOfThisMonth);

        // ============== 热门套餐 ==================
        //hotSetmeal
        List<Map<String,Object>> hotSetmeal = orderDao.findHotSetmeal();
        Map<String, Object> map = new HashMap<String,Object>();
        map.put("reportDate",today);
        map.put("todayNewMember",todayNewMember);
        map.put("totalMember",totalMember);
        map.put("thisWeekNewMember",thisWeekNewMember);
        map.put("thisMonthNewMember",thisMonthNewMember);
        map.put("todayOrderNumber",todayOrderNumber);
        map.put("todayVisitsNumber",todayVisitsNumber);
        map.put("thisWeekOrderNumber",thisWeekOrderNumber);
        map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        map.put("thisMonthOrderNumber",thisMonthOrderNumber);
        map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        map.put("hotSetmeal",hotSetmeal);

        return map;
    }
}
